<?php
class ergebnisse_model extends CI_Model {

	public function __construct()
	{
		parent:: __construct();
		$this->load->database();
	}
	
	public function get($schuelerid,$klausurenid,$fragenid)
	{
		$this -> db -> where('SchuelerID', $schuelerid);
		$this -> db -> where('KlausurenID', $klausurenid);
		$this -> db -> where('FragenID', $fragenid);

		$query = $this -> db -> get('ergebnisse');
		return $query->row_array();
	}
	
	public function get_ergebnisse()
	{
		$query = $this->db->get('ergebnisse');
		return $query->result_array();				
	}
	
	public function get_results($klausurenid,$fragenid)
	{
		$this -> db -> where('FragenID', $fragenid);
		$this -> db -> where('KlausurenID', $klausurenid);

		$query = $this -> db -> get('ergebnisse');
		return $query->result_array();
	}
	
	public function get_ergebnisse_id($schuelerid,$klausurenid,$fragenid)
	{
		$this -> db -> where('SchuelerID', $schuelerid);
		$this -> db -> where('KlausurenID', $klausurenid);
		$this -> db -> where('FragenID', $fragenid);

		$query = $this -> db -> get('ergebnisse');
		
		if ($query->num_rows() > 0){
			return TRUE;
		}else{
			return FALSE;
		}
	}
	public function set_ergebnisse($schuelerid,$klausurenid,$fragenid,$points)
	{
		$this->load->helper('url');
		
		if($this->session->userdata('antwort_data'))
   		{
 			$antwort = $this->session->userdata('antwort_data');
			if($antwort['Click'] == TRUE)
			{
				$sess_array = array( 'Antwort' => 0,
									 'Antwort2' => 0,
									 'Antwort3' => 0,
									 'TextAntwort' => '',
	    					 'Click' => FALSE );
	    		$this->session->set_userdata('antwort_data', $sess_array);
			} else {
				return;
			}
		} else {
			return;
		}

		$data = array(
			'SchuelerID' => $schuelerid,
			'KlausurenID' => $klausurenid,
			'FragenID' => $fragenid,
			'Antwort1' => $antwort['Antwort'],
			'Antwort2' => $antwort['Antwort2'],
			'Antwort3' => $antwort['Antwort3'],
			'TextAntwort' => $antwort['TextAntwort'],
			'Punkte' => $points
		);

		$this->db->insert('ergebnisse', $data);
		return;
	}
	
	public function validate_ergebnisse($schuelerid,$klausurenid,$fragenid)
	{
		$this->load->helper('url');

		if($this->session->userdata('antwort_data'))
   		{
   			$antwort = $this->session->userdata('antwort_data');
			if($antwort['Click'] == TRUE)
			{
				$antwort1 = $antwort['Antwort'];
				$sess_array = array( 'Antwort' => 0,
									 'Antwort2' => 0,
									 'Antwort3' => 0,
									 'TextAntwort' => '',
	    					 'Click' => FALSE );
	    		$this->session->set_userdata('antwort_data', $sess_array);
			} else {
				return;
			}
		} else {
			return;
		}
		$data = array(
			'Antwort1' => $antwort['Antwort'],
			'Antwort2' => $antwort['Antwort2'],
			'Antwort3' => $antwort['Antwort3'],
			'TextAntwort' => $antwort['TextAntwort'],
			'Punkte' => $this->input->post('Punkte')
		);

		$where = array('SchuelerID' => $schuelerid,'KlausurenID' => $klausurenid,'FragenID' => $fragenid);
		return  $this->db->update('ergebnisse',$data,$where);
	}
	
	public function setpoints($schuelerid,$klausurenid,$fragenid,$points,$comment)
	{
		$data = array(
			'ErreichtePunkte' => $points,
			'Kommentar' => $comment
		);
		$where = array('SchuelerID' => $schuelerid,'KlausurenID' => $klausurenid,'FragenID' => $fragenid);
		return  $this->db->update('ergebnisse',$data,$where);
	}
	
	public function delete($schuelerid,$klausurenid,$fragenid)
	{
		$this -> db -> where('SchuelerID', $schuelerid);
		$this -> db -> where('KlausurenID', $klausurenid);
		$this -> db -> where('FragenID', $fragenid);
		$this -> db -> delete('ergebnisse');
	}
}
?>